class Solution {
public:
    int next[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
    int m, n;
    int bfs(vector<vector<int>>& grid, int i, int j)
    {
        int ret = 0;
        queue<pair<int, int>> q;
        grid[i][j] = -1;
        q.push({ i,j });
        while (!q.empty())
        {
            auto [x, y] = q.front();
            q.pop();
            ret++;
            for (auto& e : next)
            {
                int nextx = e[0] + x, nexty = e[1] + y;
                if (nextx < 0 || nexty < 0 || nextx >= m || nexty >= n || grid[nextx][nexty] != 1) continue;
                grid[nextx][nexty] = -1;
                q.push({ nextx,nexty });
            }
        }
        return ret;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int ret = 0;
        m = grid.size(), n = grid[0].size();

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (grid[i][j] == 1)
                {
                    ret = max(bfs(grid, i, j), ret);
                }
            }
        }
        return ret;
    }
};